package com.example.demodb.controller;

import com.example.demodb.entity.CommonResult;
import com.example.demodb.entity.SysAccount;
import com.example.demodb.service.SysAccountService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SysAccountController {
    @Resource
    private SysAccountService sysAccountService;

    @PostMapping("/SysAccountController/findUserByAccountNameAndPassword")
    @CrossOrigin
    public CommonResult<SysAccount> findUserByAccountNameAndPassword(@RequestBody SysAccount sysAccount){
        SysAccount sa = sysAccountService.findUserByAccountNameAndPassword(sysAccount);
        if(sa == null){
            return new CommonResult<>(404,"用户名或密码错误",null);
        }
        return new CommonResult<>(200,"登陆成功",sa);
    }



    @GetMapping("/SysAccountController/findAll")
    @CrossOrigin
    public List<SysAccount> findAll(){
        return sysAccountService.findAll();
    }

    @PostMapping("/SysAccountController/addSysAccount")
    @CrossOrigin
    public CommonResult<SysAccount> addSysAccount(@RequestBody SysAccount sysAccount){
        SysAccount sa = sysAccountService.addSysAccount(sysAccount);
        if(sa == null){
            return new CommonResult<>(404,"注册失败，用户名或手机号重复",null);
        }
        return new CommonResult<>(200,"注册成功",sa);
    }
}
